package com.becca.cake.util;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

/**
 * json序列化工具类
 * 
 * @author hejinguo
 * @version $Id: JsonUtil.java, v 0.1 2011-8-9 下午06:37:39 hejinguo Exp $
 */
public class JsonUtil {
    private static SerializeConfig mapping = new SerializeConfig();

    static {
        mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        mapping.put(Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * <p>List数据转为JSON字符串，无分页</p>
     * @author hejinguo
     * @param obj 查询结果集List
     * @return String 所需JSON字符串
     */
    public static String writeObjectNoPageJSON(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", obj);
        map.put("success", "true");
        return JSON.toJSONString(map, mapping);
    }

    /**
     * <p>List数据转为JSON字符串，分页</p>
     * @author wangyuxin
     * @param obj 查询结果集List
     * @param obj2 查询结果集总记录数
     * @return String 所需JSON字符串
     */
    public static String writeObjectPageJSON(Object obj, Object obj2) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", obj);
        map.put("totalCount", obj2);
        return JSON.toJSONString(map, mapping);
    }

    /**
     * <p>单个对象数据转为JSON字符串，EXT FORM</p>
     * @author wangyuxin
     * @param obj 单条结果
     * @return String 所需JSOIN字符串
     */
    public static String writeObjectFormJSON(Object obj) {
        Object[] arrObj = new Object[1];
        arrObj[0] = obj;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", "true");
        map.put("data", arrObj);
        return JSON.toJSONString(map, mapping);
    }

    /**
     * 对象转换成JSON字符串
     * @author  hejinguo
     */
    public static String writeObjectJSON(Object obj) {
        return JSON.toJSONString(obj, mapping);
    }

    /**
     * json转换成Object
     * @param <T>
     * @param jsonData
     * @param cla
     * @return
     */
    public static final <T> T jsonToObject(String jsonData, Class<T> cla) {
        return JSON.parseObject(jsonData, cla);
    }

    /**
     * 返回操作成功消息
     * @param messages
     * @return
     */
    public static String returnJsonSuccess(String messages) {
        return "{\"success\":true,\"data\":\"" + messages + "\"}";
    }

    /**
     * 返回操作失败消息
     * @param messages
     * @return
     */
    public static String returnJsonSpecialUnSuccess(String messages) {
        return "{\"success\":false,\"data\":\"" + messages + "\"}";
    }

    /**
     * 返回失败的消息
     * @param messages
     * @return
     */
    public static String returnJsonUnSuccess(String messages) {
        return "{\"failure\":true,\"data\":\"" + messages + "\"}";
    }

    /**
     * json字符串转为list
     * @param <T>
     * @param jsonData
     * @param class1
     * @return
     */
    public static <T> List<T> returnObjectList(String jsonData, Class<T> class1) {
        List<T> list = (List<T>) JSON.parseArray(jsonData, class1);
        return list;
    }
}